package interfaces.hud.targetInfos;

import logic.common.player.Player;
import logic.ships.hunter.Hunter;
import logic.weapons.WeaponType;
import logic.weapons.energyWeapons.EnergyWeapon;

import ai.AITools;

import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import com.jme.system.DisplaySystem;

public class AimPointerController extends Controller {
	
	private static final long serialVersionUID = 1L;
	
	private AimPointer pointer;
	
	public AimPointerController(AimPointer pointer) {
		this.pointer = pointer;
	}
	
	@Override
	public void update(float time) {
		Hunter hunter = pointer.getHunter();
		Player player = pointer.getPlayer();
		
		boolean targetVisible = hunter != null && !hunter.isDestroyed() && hunter.getParent() != null 
		&& player.getHunter() != null && !player.getHunter().getWeapons(WeaponType.Energy).isEmpty() 
		&& hunter.getLocalTranslation().distance(player.getHunter().getLocalTranslation()) <= 250f;
		
		pointer.setVisible(targetVisible);
		if(!targetVisible) return;
		
		EnergyWeapon energyWeapon = (EnergyWeapon)player.getHunter().getWeapons(WeaponType.Energy).get(0);
		
		Vector3f aimLoc = AITools.getEnergyFireLocation(energyWeapon, hunter);
		Vector3f aimScreenPos = DisplaySystem.getDisplaySystem().getScreenCoordinates(aimLoc);
		pointer.getLocalTranslation().set(aimScreenPos);
	}
}